package main

import (
	"fmt"
	"time"
)

func intervalIntersection(firstList [][]int, secondList [][]int) [][]int {

	// 判断firstList  第一个到第n和 secondList所有的的交集
	firstListRow := len(firstList)
	secondListRow := len(secondList)
	if firstListRow > secondListRow {
		return intervalIntersection(secondList, firstList)
	}
	result := [][]int{}
	for i := 0; i < firstListRow; i++ {
		for j := 0; j < secondListRow; j++ {
			array := []int{}
			//给出最小的区间点
			// fmt.Println(firstList[i][0], firstList[i][1], secondList[j][0], secondList[j][1])
			if firstList[i][0] >= secondList[j][0] && firstList[i][0] <= secondList[j][1] {
				array = append(array, firstList[i][0])
			} else if secondList[j][0] > firstList[i][0] && secondList[j][0] <= firstList[i][1] {
				array = append(array, secondList[j][0])
			} else {
				continue
			}
			//给出最大的区间点
			if firstList[i][1] >= secondList[j][0] && firstList[i][1] <= secondList[j][1] {
				array = append(array, firstList[i][1])
			} else if secondList[j][1] >= firstList[i][0] && secondList[j][1] <= firstList[i][1] {
				array = append(array, secondList[j][1])
			} else {
				continue
			}

			result = append(result, array)
			// fmt.Println("i=", i, "result=", result, "array=", array)

		}

	}
	// fmt.Println(result)
	return result
}

func main() {
	start := time.Now()
	firstList := [][]int{{0, 2}, {5, 10}, {13, 23}, {24, 25}}
	secondList := [][]int{{1, 5}, {8, 12}, {15, 24}, {25, 26}}
	result := intervalIntersection(firstList, secondList)
	cost := time.Since(start)
	fmt.Println("result:", result)
	fmt.Println(cost)

}
